package org.example.actualcombat.demos.web.modules.pms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.actualcombat.demos.web.common.api.CommonPage;
import org.example.actualcombat.demos.web.common.api.CommonResult;
import org.example.actualcombat.demos.web.modules.pms.model.PmsProductAttributeModel;
import org.example.actualcombat.demos.web.modules.pms.model.dto.RelationAttrInfoDTO;
import org.example.actualcombat.demos.web.modules.pms.service.PmsProductAttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
* 商品分类/ 属性列表和参数列表（属于子列表）
* */
@RestController
@RequestMapping("/productAttribute")
public class PmsProductAttributeController {
    @Autowired
    PmsProductAttributeService pmsProductAttributeService;

    /*
    * 列表
    * */
    @RequestMapping(value = "/list2/{cid}",method = RequestMethod.GET)
    public CommonResult getList(@PathVariable int cid, @RequestParam(value = "type")int type, @RequestParam(value = "pageNum") int pageNum, @RequestParam(value = "pageSize" )int pageSize){

        Page page =   pmsProductAttributeService.listSelf(cid,type,pageNum,pageSize);
        return CommonResult.success(CommonPage.restPage(page));
    }
    /*
    * 新增
    *       url:'/productAttribute/create',
            method:'post',
            data:data
    * */
    @RequestMapping(value = "/createProduct",method = RequestMethod.POST)
    public CommonResult created(@RequestBody PmsProductAttributeModel pmsProductAttributeModel){
        boolean save = pmsProductAttributeService.save2(pmsProductAttributeModel);
        if(save){
            return CommonResult.success("保存成功");
        }else{
            return CommonResult.failed("保存失败");
        }
    }

    /*
    *     url:'/productAttribute/attrInfo/'+productCategoryId,
    method:'get'
    * */
    @RequestMapping(value = "/attrInfo/{cid}",method = RequestMethod.GET)
    public CommonResult getRelationAttrInfoByCid(@PathVariable Long cid){
//        QueryWrapper<RelationAttrInfoDTO> queryWrapper = new QueryWrapper<>();
//        queryWrapper.lambda().eq(RelationAttrInfoDTO::getSubjectId,cid);
        List<RelationAttrInfoDTO> list = pmsProductAttributeService.getRelationAttrInfoByCid(cid);

        return CommonResult.success(list);
    }

































    @RequestMapping(value = "/update/{id}",method = RequestMethod.POST)
    public CommonResult update(@PathVariable long id, @RequestBody PmsProductAttributeModel pmsProductAttributeModel){
        UpdateWrapper<PmsProductAttributeModel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(PmsProductAttributeModel::getId,id);
        boolean save = pmsProductAttributeService.update(pmsProductAttributeModel,updateWrapper);
        if(save){
            return CommonResult.success("保存成功");
        }else{
            return CommonResult.failed("保存失败");
        }
    }

    /*
    * 查询单个基础信息 -- 还没检查
    *   url:'/productAttribute/'+id,
        method:'get'
    * */
    @RequestMapping(value = "/getOne/{id}",method = RequestMethod.GET)
    public CommonResult<PmsProductAttributeModel> getOne(@PathVariable long id){
        PmsProductAttributeModel id1 = pmsProductAttributeService.getById(id);
        return CommonResult.success(id1);
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public CommonResult<String> deleteOne(@RequestParam(value = "ids") List<Long> ids){
        boolean b = pmsProductAttributeService.removeIds(ids);
        if(b){
            return  CommonResult.success("成功");
        }else{
            return  CommonResult.failed("失败");
        }
    }











































}


